<template>
  <el-dialog v-model="visible" :title="dialogTitle" width="600px" @close="$emit('close')">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="工单标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入工单标题" />
      </el-form-item>
      <el-form-item label="工单类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option label="故障报修" value="bug" />
          <el-option label="功能建议" value="feature" />
          <el-option label="咨询问题" value="question" />
          <el-option label="投诉建议" value="complaint" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="form.priority" placeholder="请选择优先级">
          <el-option label="紧急" value="urgent" />
          <el-option label="高" value="high" />
          <el-option label="中" value="medium" />
          <el-option label="低" value="low" />
        </el-select>
      </el-form-item>
      <el-form-item label="分配客服" prop="assignee">
        <el-select v-model="form.assignee" placeholder="请选择客服">
          <el-option v-for="a in assignees" :key="a.value" :label="a.label" :value="a.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户姓名" prop="customer">
        <el-input v-model="form.customer" placeholder="请输入客户姓名" />
      </el-form-item>
      <el-form-item label="工单描述" prop="description">
        <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请详细描述问题或建议" />
      </el-form-item>
      <el-form-item label="标签">
        <el-input v-model="form.tagsStr" placeholder="请输入标签，用逗号分隔" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="onSubmit">保存工单</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
const props = defineProps({
  ticket: { type: Object, default: null },
  assignees: { type: Array, required: true }
})
const emit = defineEmits(['save', 'close'])
const visible = ref(true)
const formRef = ref()
const form = reactive({
  id: '',
  title: '',
  type: '',
  priority: '',
  assignee: '',
  customer: '',
  description: '',
  tagsStr: ''
})
const rules = {
  title: [{ required: true, message: '请输入工单标题', trigger: 'blur' }],
  type: [{ required: true, message: '请选择工单类型', trigger: 'change' }],
  priority: [{ required: true, message: '请选择优先级', trigger: 'change' }],
  assignee: [{ required: true, message: '请选择分配客服', trigger: 'change' }],
  customer: [{ required: true, message: '请输入客户姓名', trigger: 'blur' }],
  description: [{ required: true, message: '请输入工单描述', trigger: 'blur' }]
}
const dialogTitle = computed(() => form.id ? '编辑工单' : '新建工单')
watch(() => props.ticket, (val) => {
  if (val) {
    Object.assign(form, val)
    form.tagsStr = Array.isArray(val.tags) ? val.tags.join(',') : ''
  } else {
    Object.assign(form, { id: '', title: '', type: '', priority: '', assignee: '', customer: '', description: '', tagsStr: '' })
  }
  visible.value = true
}, { immediate: true })
function onSubmit() {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      const tags = form.tagsStr.split(',').map(s => s.trim()).filter(Boolean)
      emit('save', { ...form, tags })
    }
  })
}
</script>

<style scoped>
</style> 